package com.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.common.R;
import com.erp.entity.Department;
import com.erp.entity.Payable;
import com.erp.service.DepartmentService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/department")
@RequiresRoles(value = {"HrAdministrator","SupperAdministrator"},logical = Logical.OR)
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping
    public R<String> add(@RequestBody Department department){
        boolean save = departmentService.save(department);
        if (!save) {
            return  R.error("error");
        }
        return R.success("部门新增成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Department department){
        boolean b = departmentService.updateById(department);
        if (b){
            return R.success("更新成功");
        }else {
            return R.error("更新失败");
        }
    }

    @DeleteMapping("/delete")
    public R<String> delete(Long id) {
        boolean b = departmentService.removeById(id);
        return  R.success("部门删除成功");
    }

    @GetMapping("/find")
    public R<Department> find(Long id) {
        Department byId = departmentService.getById(id);
        return R.success(byId);
    }


    @GetMapping
    public R<Page<Department>> findpage(int page, int pageSize) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Department> departmentPage = new Page<>(page, pageSize);
        departmentService.page(departmentPage, lambdaQueryWrapper);
        return R.success(departmentPage);

    }




}
